package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IUnicomRegionService;
import com.iwhalecloud.bss.kite.cucc.common.util.StrTools;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.LocalProvinceAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @ClassName : LocalProvinceAttrLinkHandler
 * @Description : 装机地址省份属性联动处理类
 * @Author : xie.zhengjun
 * @Date : 2020/9/22 17:12
 */
@Service
@AttrHandler(LocalProvinceAttrHandler.class)
public class LocalProvinceAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Resource
    private IUnicomRegionService unicomRegionService;

    @Resource
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String cityFieldName = null;
        if ("2_0000107".equals(attr.getFieldName())) {
            //城市（Z端）
            cityFieldName = "2_0000109";
        }
        else if ("2_0000220".equals(attr.getFieldName())) {
            //城市
            cityFieldName = "2_0000221";
        }

        if (StrTools.isEmpty(cityFieldName)) {
            return;
        }

        KiteAttr city = inst.getAttr(cityFieldName);
        if (Objects.isNull(city)) {
            return;
        }
        // 得到5G产品的，判断是否5G产品,如果省份是上海则默认地市为上海市
        if ("100000000017364".equals(inst.getSpecId()) || "100000000017326".equals(inst.getSpecId())) {
            if ("31".equals(attr.getValue()) && "2_0000220".equals(attr.getFieldName())){
                // 上海默认上海市
                operateInstAttrService.setAttrValue(inst, cityFieldName, "310", data.getSceneInstId(), null);
            }
            else {
                // 省份改动则清空地市
                operateInstAttrService.setAttrValue(inst, cityFieldName, "", data.getSceneInstId(), null);
            }
        }
        else {
            // 省份改动则清空地市
            operateInstAttrService.setAttrValue(inst, cityFieldName, "", data.getSceneInstId(), null);
        }
        // 选定省份后联动出可选地市
        LocalAttrHelper.loadPlatformCity(attr.getValue(), city, inst.getSpecId());
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
